#include "dataBase.h"
int getFileSize(MYSQL *mysql, const char *file_name, 
    const char *file_path, int *file_size) {
    MYSQL_RES* res;
    MYSQL_ROW row;
    int query_ret;
    char query[4096] = {0};
    sprintf(query,"select file_size from FileSystem where file_name = '%s' and file_path = '%s';",
        file_name,file_path);
    query_ret = mysql_query(mysql,query);
    
    if(query_ret != 0) {
        return -1;
    }
    res = mysql_store_result(mysql);
    int num_size = mysql_num_rows(res);
    if(num_size == 1) {
        row = mysql_fetch_row(res);
        *file_size = atoi(row[0]);
        return 0;
    }

    return -1;
}

// 根据 file_name、file_path查询文件哈希，通过file_hash返回结果
int getFileHash(MYSQL *mysql, const char *file_name, 
    const char *file_path, char *file_hash) {

    MYSQL_RES* res;
    MYSQL_ROW row;
    int query_ret;
    char query[4096] = {0};
    sprintf(query,"select file_hash from FileSystem where file_name = '%s' \
        and file_path = '%s' and completed_file;",
        file_name,file_path);
    query_ret = mysql_query(mysql,query);
    
    if(query_ret != 0) {
        return -1;
    }
    res = mysql_store_result(mysql);
    int num_size = mysql_num_rows(res);
    if(num_size == 1) {
        row = mysql_fetch_row(res);
        memcpy(file_hash,row[0],strlen(row[0]));
        return 0;
    }

    return -1;
    
}

